package com.rslai.base.tool.servertest.command;

import com.rslai.base.tool.autotest.mock.ExpectationBuilder;
import com.rslai.base.tool.autotest.mock.Mocks;
import com.rslai.base.tool.autotest.mock.model.Callback;
import com.rslai.base.tool.autotest.mock.model.Expectation;
import com.rslai.base.tool.autotest.mock.model.HttpExpectation;
import com.rslai.base.tool.autotest.mock.model.RpcExpectation;
import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.model.MockInfo;
import com.rslai.base.tool.servertest.response.Response;
import com.rslai.base.tool.servertest.util.CloneUtil;
import com.rslai.base.tool.servertest.util.PropertyUtils;
import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.response.Response;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MockStepCommand extends ParameterizedCommand {
    private MockInfo mockInfo;
    private Expectation expectation;

    public MockStepCommand(List<KeyValueStore> params) {
        super(params);
    }

    protected Response doExecuteInternal(Response preResult, List<KeyValueStore> processedParams, Context context) throws Throwable {
        if (processedParams == null) {
            return preResult;
        }
        this.mockInfo = new MockInfo(processedParams);
        this.expectation = this.mockInfo.createExpectation();
        Mocks.whenCall(this.mockInfo.getService()).withProductNo(this.mockInfo.getTarget()).withSource(this.mockInfo.getSource()).withKey(this.mockInfo.getKey()).withMockServer(PropertyUtils.getProperty("mockserver", "l-qtest25.tc.beta.cn6.qunar.com")).thenReturn(this.expectation);

        return preResult;
    }

    public StepCommand doClone() {
        return new MockStepCommand(CloneUtil.cloneKeyValueStore(this.params));
    }

    public Map<String, Object> toReport() {
        Map details = new HashMap();
        details.put("stepName", "设置期望:");
        details.put("name", String.format("当来自%s的应用调用业务线%s下的接口%s,并且标识是%s返回如下数据", new Object[]{this.mockInfo.getSource(), this.mockInfo.getTarget(), this.mockInfo.getService(), this.mockInfo.getKey()}));

        details.put("params", getReturnValues(this.expectation));
        return details;
    }

    private List<KeyValueStore> getReturnValues(Expectation expectation) {
        if (expectation == null) return null;
        if ((expectation instanceof RpcExpectation)) {
            return Arrays.asList(new KeyValueStore[]{new KeyValueStore("return", expectation.getBody())});
        }
        List params = new ArrayList();
        params.add(new KeyValueStore("return", expectation.getBody()));
        Callback callback = ((HttpExpectation) expectation).getCallback();
        if (callback != null) {
            params.add(new KeyValueStore("callback_url", callback.getUrl()));
            params.add(new KeyValueStore("callback_body", callback.getBody()));
        }
        return params;
    }
}